# File contains tools for "Not A Question" post text quantification

import re
from Resources.config import TOO_LOCALIZED_QUANTIFIER_SPLIT
from Resources.datetime_regexps import DATETIME_REGEXP_LIST
#from Resources.geographic_names import GEOGRAPHIC_NAMES

GEOGRAPHIC_NAMES = []
f_in = open('Resources/geographic_names.txt', 'r')
for line in f_in:
	GEOGRAPHIC_NAMES.append(line[:-1]) # Strips tailing '\n' character

# Will return true if element of GEOGRAPHIC_NAMES is present within the text
def GEO_Name_Present(text):
	
	present = False
	text_low = text.lower()
	
	for geo_name in GEOGRAPHIC_NAMES:
		if geo_name in text_low:
			present = True
			break

	return present

# Will return true if text matches one of paterns from DATETIME_REGEXP_LIST
def datetime_regexpMatch(text):

	match = False

	for pattern in DATETIME_REGEXP_LIST:
		if re.match(pattern, text, re.M|re.I):
			match = True
			break

	return match

def getPostTagsNum(Post):
	result = 0
	if Post.tag1: result += 1
	if Post.tag2: result += 1
	if Post.tag3: result += 1
	if Post.tag4: result += 1
	if Post.tag5: result += 1
	return result

# Will return sum of eligible parts of quantifier split
def quantify(Post):
	result = 0

	if not GEO_Name_Present(Post.bodyMarkdown):
		result += TOO_LOCALIZED_QUANTIFIER_SPLIT['NO_GEO_REFERENCE']

	if not datetime_regexpMatch(Post.bodyMarkdown):
		result += TOO_LOCALIZED_QUANTIFIER_SPLIT['NO_DATETIME']
	
	result += TOO_LOCALIZED_QUANTIFIER_SPLIT['HASHTAGS_NUMBER'][getPostTagsNum(Post)]	

	return result 

# Everything below is for testing purposes only. It is also part of QuantifiersAssessment.py
"""
import Parser
import Post
import FileIO

fileStream = FileIO.openTrainingFile(FileIO.READ_MODE)
Parser.readHeader(fileStream)

with_GEO_name = 0
without_GEO_name = 0

datetime_matched_regexp = 0
datetime_unmatched_regexp = 0

overall_TL_quantified_val_acc = 0
counter = 0

open_TL_quantified_val_acc = 0
open_counter = 0

closed_TL_quantified_val_acc = 0
closed_counter = 0

# TL = Too localized
TL_quantified_val_acc = 0
TL_counter = 0

average_hashtags_num = 0
average_open_hashtags_num = 0
average_closed_hashtags_num = 0
average_TL_closed_hashtags_num = 0

while (not Parser.isEndOfFile(fileStream)):
	
	TL_quantified_val = 0
	newPost = Parser.readPost(fileStream)
	
	bodyMarkdown = newPost.bodyMarkdown

	GEO_name_match = GEO_Name_Present(bodyMarkdown)
	datetime_regexp_match = datetime_regexpMatch(bodyMarkdown)
	hashtags_num = getPostTagsNum(newPost)

	if not GEO_name_match:
		with_GEO_name += 1
		TL_quantified_val += TOO_LOCALIZED_QUANTIFIER_SPLIT['NO_GEO_REFERENCE']
	else:
		without_GEO_name += 1

	if not datetime_regexp_match:
		datetime_matched_regexp += 1
		TL_quantified_val += TOO_LOCALIZED_QUANTIFIER_SPLIT['NO_DATETIME']
	else:
		datetime_unmatched_regexp += 1

	overall_TL_quantified_val_acc += TL_quantified_val
	average_hashtags_num += hashtags_num
	counter += 1

	post_status = Post.getFullPostStatus( newPost.openStatus )

	if ( post_status == "open"):
		open_TL_quantified_val_acc += TL_quantified_val
		average_open_hashtags_num += hashtags_num
		open_counter += 1
	else:
		closed_TL_quantified_val_acc += TL_quantified_val
		average_closed_hashtags_num += hashtags_num
		closed_counter += 1

	if ( post_status == "too localized"):
		TL_quantified_val_acc += TL_quantified_val
		average_TL_closed_hashtags_num += hashtags_num
		TL_counter += 1 

	print "Post - ", counter, "; GEO_name - ", GEO_name_match, "; datetime regexp match - ", datetime_regexp_match, "; Tags_num - ", hashtags_num, "; Post status - \'" + post_status + "\'"

import datetime
f = open("Output/TL_quantifier_" + datetime.datetime.now().strftime("%Y%m%d_%H%M%S") + ".log", 'w')

f.write( "Current quantifier config:\n\nTOO_LOCALIZED_QUANTIFIER_SPLIT = {")

for key in TOO_LOCALIZED_QUANTIFIER_SPLIT:
	f.write( "\n\t\'" + str(key) + "\' : " + str(TOO_LOCALIZED_QUANTIFIER_SPLIT[key]))

f.write( "\n}\n\nAll posts:                                      " + str(counter) )
f.write( "\n\nPosts with GEO name:                            " + str(with_GEO_name) )
f.write( "\nPosts without GEO name:                         " + str(without_GEO_name) )
f.write( "\n\nPosts matched datetime regexp:                  " + str(datetime_matched_regexp) )
f.write( "\nPosts unmatched datetime regexp:                " + str(datetime_unmatched_regexp) )
f.write( "\n\nAverage tags number value over all posts:       " + str(float(average_hashtags_num)/counter) )
f.write( "\nAverage tags number value over open posts:      " + str(float(average_open_hashtags_num)/open_counter) )
f.write( "\nAverage tags number value over closed posts:    " + str(float(average_closed_hashtags_num)/closed_counter) )
f.write( "\nAverage tags number value over TL closed posts: " + str(float(average_TL_closed_hashtags_num)/TL_counter) )
f.write( "\n\nAverage TL quantifier value over all posts:     " + str(float(overall_TL_quantified_val_acc)/counter) )
f.write( "\nAverage TL quantifier value over open posts:    " + str(float(open_TL_quantified_val_acc)/open_counter) )
f.write( "\nAverage TL quantifier value over closed posts:  " + str(float(closed_TL_quantified_val_acc)/closed_counter) )
f.write( "\nAverage TL quantifier value over TL posts:      " + str(float(TL_quantified_val_acc)/TL_counter) )

f.close()
"""